<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;

class OperationLogResource extends BaseResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'user_id' => $this->user_id,
            'username' => $this->username,
            'method' => $this->method,
            'action' => $this->action,
            'resource' => $this->resource,
            'resource_id' => $this->resource_id,
            'description' => $this->description,
            'path' => $this->path,
            'params' => $this->params,
            'before_data' => $this->before_data,
            'after_data' => $this->after_data,
            'changes' => $this->changes,
            'ip' => $this->ip,
            'user_agent' => $this->user_agent,
            'status' => $this->status,
            'message' => $this->message,
            'response' => $this->message ? json_decode($this->message, true) : null,
            'user' => $this->whenLoaded('user', function () {
                return [
                    'id' => $this->user->id,
                    'username' => $this->user->username,
                    'nickname' => $this->user->nickname,
                ];
            }),
            'created_at' => $this->formatDateTime($this->created_at),
        ];
    }
}

